﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using System.ComponentModel;
namespace WPFExcelReport
{
    public partial class TEKMU
    {
        #region ==EKPMU===========
        public static readonly DependencyProperty EKPMUProperty = DependencyProperty.Register("EKPMU",
            typeof(string), typeof(TEKMU),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnEKPMUChanged));
        public string EKPMU
        {
            get
            {
                return (string)GetValue(EKPMUProperty);
            }
            set
            {
                SetValue(EKPMUProperty, value);
             }
        }
        private static void OnEKPMUChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TEKMU;
            if (src != null)
            {
                src.EKPMU = (string)e.NewValue;
            }
        }
        #endregion

        public TEKMU()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VABAUFM();
            this.DataContext = MV; 
            InitializeComponent();
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
           
            base.vwMaster_CurrentChanged(sender, e);
            if (!IsAuto)
            {
                return;
            }
            if (dwMaster == null)
            {
                EKPMU = "";              
            }
            else
            {
                EKPMU = dwMaster["ABAAUFTYP"].ToString() + dwMaster["ABAAUFKRS"].ToString() + dwMaster["ABAAUFLFD"].ToString() + dwMaster["ABAAUFPOS"].ToString() + dwMaster["ABAAPOS"].ToString()
                  + dwMaster["ABAVKAUFTYP"].ToString() + dwMaster["ABAVKAUFKRS"].ToString() + dwMaster["ABAVKAUFLFD"].ToString() + dwMaster["ABAVKAUFPOS"].ToString()
                  + "," + dwMaster["ABARESMG"].ToString() + ";";
                string key = ORDERNUM;
                ORDERNUM = "";
                ORDERNUM = key;//重新加载本界面
            }
        }

        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            TMV.UpdateABAUFM(dwMaster, 5, app.company);
            base.SaveCommand(sender, e);
        }

        public override void OnAutoChg(bool value)
        {
            if (!value)
                return;
            vwMaster.View.MoveCurrentTo(-1);
            LOADBYAUF();
            IsAuto = false;
            base.OnAutoChg(value);
        }

        public override void LOADBYAUF()
        {
            base.LOADBYAUF();
            TBMain.Clear();
            if (ORDERNUM.Length < 11)
                return;
            PM["ORDERNUM"] = ORDERNUM;
            PM["AUFTYP"] = ORDERNUM.Substring(11, 1);
            PM["KRS"] = ORDERNUM.Substring(0, 2);
            PM["LFD"] = ORDERNUM.Substring(2, 6);
            PM["POS"] = ORDERNUM.Substring(8, 3);
            TMV.LOAD(PM);            
            AUTOCHK(); 
        }

        public void AUTOCHK()
        {
            if (!IsAuto)
                return; 
            vwMaster.View.MoveCurrentTo(-1);
            int count = TBMain.Rows.Count;
            for (int i = 0; i < count; i++)
            {
                vwMaster.View.MoveCurrentToNext();
            } 
        }
    }
}
